package com.ynet.middleground.oss.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ynet.middleground.oss.entity.OauthUserAuthentication;
import com.ynet.middleground.oss.oauth.service.OauthUserAuthenticationService;
import com.ynet.middleground.riskmanage.bean.CreateOauthUserReq;

/**
 * @author Suremotoo
 * @description 注册用户，TODO，测试，暂不使用
 * @create 2020-10-13 09:36:23
 */
@RestController
@RequestMapping("/registry")
public class RegistryController {

    @Autowired
    private OauthUserAuthenticationService oauthUserAuthenticationService;

    @Autowired
    private BCryptPasswordEncoder encoder;

    /**
     * 注册用户
     *
     * @param userAuthentication 用户信息
     * @return
     */
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public boolean createUser(@RequestBody CreateOauthUserReq userAuthentication) {
        if (!"".equals(userAuthentication.getPassword()) && !"".equals(userAuthentication.getUsername())) {
            OauthUserAuthentication oauthUserAuthentication = new OauthUserAuthentication();
            oauthUserAuthentication.setUsername(userAuthentication.getUsername());
            oauthUserAuthentication.setPassword(encoder.encode(userAuthentication.getPassword()));
            oauthUserAuthentication.setIsAccountNonExpired(true);
            oauthUserAuthentication.setIsAccountNonLocked(true);
            oauthUserAuthentication.setIsCredentialsNonExpired(true);
            oauthUserAuthentication.setIsEnabled(true);
            /* if (userAuthentication.getAuthorities() != null && !userAuthentication.getAuthorities().isEmpty()) {
                oauthUserAuthentication.setAuthorities(userAuthentication.getAuthorities());
            }*/
            return oauthUserAuthenticationService.save(oauthUserAuthentication);
        }
        return false;
    }

}
